package com.jumi.microservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.common.base.dto.BasePageResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.constant.RequestConstant;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.constant.StatusEnum;
import com.jumi.microservice.domain.ImageSpaceDO;
import com.jumi.microservice.domain.ImageSpaceDTO;
import com.jumi.microservice.domain.ImageSpaceVO;
import com.jumi.microservice.domain.ImagesVO;
import com.jumi.microservice.service.ImageSpaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商城相册图片表 前端控制器
 * </p>
 *
 * @author liuz
 * @since 2020-07-27
 */
@RestController
@Api(tags = "商城相册图片表 相关接口")
@RequestMapping("/commodity/image/space")
public class ImageSpaceController {

    @Autowired
    private ImageSpaceService imageSpaceService;


    @ApiOperation(value = "新增数据", notes = "新增一条商城相册图片表数据")
    @PostMapping("/add")
    public ResponseResult add(@RequestBody ImagesVO imageSpace, @RequestHeader(RequestConstant.ADMIN_NAME) String username) {
        ExceptionEnum.FAIL_IMAGE_NULL.doThrowIf(imageSpace==null);
        List<ImageSpaceVO> vos = imageSpace.getImageSpaceVOS();
        ExceptionEnum.FAIL_IMAGE_NULL.doThrowIf(vos==null||vos.size()==0);
        List<ImageSpaceDTO> dtos = vos.stream().map(vo->{
            ImageSpaceDTO dto = vo2Dto(vo);
            dto.setModifier(username);
            dto.setCreater(username);
            dto.setPhotoAlbumId(imageSpace.getPhotoAlbumId());
            return dto;
        }).collect(Collectors.toList());
        imageSpaceService.insert(dtos);
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "删除数据", notes = "删除指定ID商城相册图片表数据")
    @ApiImplicitParam(name = "ids", value = "IDS")
    @DeleteMapping("/delete")
    public ResponseResult delete(@RequestParam String ids) {
        imageSpaceService.delete(ids);
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "更新数据", notes = "更新指定ID商城相册图片表数据")
    @PutMapping("/update")
    public ResponseResult update(@RequestBody ImageSpaceVO imageSpace, @RequestHeader(RequestConstant.ADMIN_NAME) String username) {
        ImageSpaceDTO dto = vo2Dto(imageSpace);
        dto.setModifier(username);
        imageSpaceService.updateIg(dto);
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "查询数据", notes = "获取指定ID商城相册图片表数据")
    @ApiImplicitParam(name = "id", value = "ID")
    @GetMapping("/detail")
    public ResponseResult<ImageSpaceDO> detail(@RequestParam Integer id) {
        ImageSpaceDO imageSpace = imageSpaceService.getById(id);
        return ResultGenerator.genSuccessResult(imageSpace);
    }

    @ApiOperation(value = "转移相册", notes = "图片转移相册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "photoAlbumId", value = "相册id"),
            @ApiImplicitParam(name = "ids", value = "图片ids"),
    })
    @PutMapping("/change")
    public ResponseResult change(@RequestParam Long photoAlbumId, @RequestParam String ids,@RequestHeader(RequestConstant.ADMIN_NAME) String username) {
        imageSpaceService.change(photoAlbumId, ids, username);
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "查询分页", notes = "获取商城相册图片表分页列表数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "当前页码"),
        @ApiImplicitParam(name = "size", value = "分页大小"),
    })
    @GetMapping("/list")
    public BasePageResponse list(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "0") Integer size,
            ImageSpaceVO imageSpace) {
        LambdaQueryWrapper<ImageSpaceDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ImageSpaceDO::getPhotoAlbumId,imageSpace.getPhotoAlbumId());
        if(StatusEnum.STATUS_YES.getCode()==imageSpace.getOrderType()) {
            wrapper.orderByAsc(ImageSpaceDO::getUploadTime);
        }else {
            wrapper.orderByDesc(ImageSpaceDO::getUploadTime);
        }
        IPage<ImageSpaceDO> pageInfo = imageSpaceService.page(
                new Page<>(page, size),wrapper);
        return ResultGenerator.genSuccessResultPage(pos2Vos(pageInfo.getRecords()),pageInfo.getTotal());
    }

    /**
     * ovo->dto
     * @param vo
     * @return
     */
    private ImageSpaceDTO vo2Dto(ImageSpaceVO vo){
        ImageSpaceDTO dto = null;
        try {
            dto = vo.clone(ImageSpaceDTO.class);
        }catch (Exception e){

        }
        return dto;
    }

    /**
     * dto->vo
     * @param dto
     * @return
     */
    private ImageSpaceVO dto2Vo(ImageSpaceDTO dto){
        ImageSpaceVO vo = null;
        try {
            vo = dto.clone(ImageSpaceVO.class);
        }catch (Exception e){
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return vo;
    }

    /**
     * 列表对象转换
     * @param pos
     * @return
     */
    private List<ImageSpaceVO> pos2Vos(List<ImageSpaceDO> pos){
        return pos.stream().map(p->{
            ImageSpaceVO vo = null;
            try {
                vo = p.clone(ImageSpaceVO.class);
            }catch (Exception e){
                ExceptionEnum.FAIL_CHANGE.newException();
            }
            return vo;
        }).collect(Collectors.toList());
    }
}
